package com.goldenlight.fvms.manager.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.goldenlight.fvms.manager.entity.UserInfo;
import com.goldenlight.fvms.manager.service.UserInfoService;
import com.goldenlight.fvms.manager.vo.Response;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  用户信息表 前端控制器
 * </p>
 *
 * @author luocx
 * @since 2018-12-08
 */
@RestController
@RequestMapping("/userInfo")
@Api("管理员 API")
public class UserInfoController {
    @Autowired
    private UserInfoService userInfoService;

    @ApiOperation(value = "根据username查询用户信息", notes = "查询数据库中某个用户的信息")
    @PostMapping("/login")
    public Response login(@RequestParam(name = "username", required = true) String username,
                          @RequestParam(name = "password", required = true) String password,
                          HttpServletRequest request, HttpServletResponse response) {
        UserInfo userInfo = userInfoService.getById(username);
        if (null == userInfo) {
            return Response.build(403, "该用户不存在", null);
        }else {
            if (password.equals(userInfo.getPassword())) {
                Map<String, Object> token = new HashMap<>();
                token.put("username", username);
                token.put("password", password);
                return Response.build(200,"登录成功",token);
            }else {
                return Response.build(403, "用户名或密码不正确", null);
            }
        }
    }


    static class User{
        private String username;
        private String password;

        public String getUsername() {
            return username;
        }

        public void setUsername(String username) {
            this.username = username;
        }

        public String getPassword() {
            return password;
        }

        public void setPassword(String password) {
            this.password = password;
        }

        @Override
        public String toString() {
            return "User{" +
                    "username='" + username + '\'' +
                    ", password='" + password + '\'' +
                    '}';
        }
    }


}

